/**
 * Created by Administrator on 2017/12/13.
 *//*

window.onload = function () {
    //ͷ������
    var search = document.querySelector('.jd_search');
    var banner = document.querySelector('.jd_banner');
    window.onscroll = function () {
        var scrollTop = document.body.scrollTop;
        var bannerWidth = banner.offsetHeight;
        if (scrollTop <= bannerWidth) {
            var opacity = scrollTop / bannerWidth;
            search.style.backgroundColor = "rgba(255, 35, 34, "+ opacity +")";
        }
    }
    //ʱ�䵹��ʱ
    var time = document.querySelector('.jd_time');
    var spans = time.querySelectorAll('span');
    var time1 = new Date();
    var time2 = new Date(2017,12,1);
    var totalTime = (time2 - time1) / 1000;
    var timer = setInterval(function () {
        totalTime--;
        if (totalTime < 0) {
            clearInterval(timer);
            return
        }
        var day = Math.floor(totalTime / 60 / 60 / 24);
        var hour = Math.floor(totalTime / 60 / 60 % 24);
        var minute = Math.floor(totalTime / 60 % 60);
        var second = Math.floor(totalTime % 60);
        spans[0].innerHTML = Math.floor(day / 10);
        spans[1].innerHTML = Math.floor(day % 10);
        spans[3].innerHTML = Math.floor(hour / 10);
        spans[4].innerHTML = Math.floor(hour % 10);
        spans[6].innerHTML = Math.floor(minute / 10);
        spans[7].innerHTML = Math.floor(minute % 10);
        spans[9].innerHTML = Math.floor(second / 10);
        spans[10].innerHTML = Math.floor(second % 10);
    },1000)

    //�ֲ�ͼ
    var imgBox = banner.querySelector('ul:first-of-type');
    var bannerWidth = banner.offsetWidth;
    var first = imgBox.querySelector('li:first-of-type');
    var last = imgBox.querySelector('li:last-of-type');
    imgBox.appendChild(first.cloneNode(true));
    imgBox.insertBefore(last.cloneNode(true),first);
    var lis = imgBox.querySelectorAll('li');
    var count = lis.length;
    for (var i = 0; i < count; i++) {
        lis[i].style.width = bannerWidth + 'px';
    }
    imgBox.style.width = count * bannerWidth + 'px';
    //λƫ��
    imgBox.style.left = -bannerWidth + 'px';
    window.onresize = function () {
        bannerWidth = banner.offsetWidth;
        for (var i = 0; i < count; i++) {
            lis[i].style.width = bannerWidth + 'px';
        }
        imgBox.style.width = count * bannerWidth + 'px';
        imgBox.style.left = -bannerWidth + 'px'
    }
    //�Զ��ֲ�
    var index = 1;
    var timerId;
    function startTime () {
        timerId = setInterval(function () {
            index++;
            imgBox.style.transition = 'left 0.5s ease-in-out';
            imgBox.style.left = -index * bannerWidth + 'px';
            //���ö�ʱ������ͼƬ�������ͼƬ�Ĺ��ʱ��
            setTimeout(function () {
                if (index == count - 1) {
                    index = 1;
                    imgBox.style.transition = 'none';
                    imgBox.style.left = -index * bannerWidth + 'px';
                }
            },500)
        },2000)
    }
    startTime()
    //С��
    function circleLight () {
        var circle = banner.querySelector('ol').querySelectorAll('li');
        var len = circle.length;
        for (var i = 0; i < len; i++) {
            circle[i].classList.remove('active');
        }
        circle[index - 1].classList.add('active');
    }
    //�ֶ��ֲ�
    var isEnd = true;
    var startX, moveX, distanceX;
    window.addEventListener('touchstart',function (e) {
        clearInterval(timerId);
        startX = e.targetTouches[0].clientX;
    })
    window.addEventListener('touchmove',function (e) {
        if (isEnd) {
            moveX = e.targetTouches[0].clientX;
            distanceX = moveX - startX;
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + distanceX + 'px';
        }
    })
    window.addEventListener('touchend',function (e) {
        isEnd = false;
        if (Math.abs(distanceX) > 100) {
            if (distanceX > 0) {
                index--;
                imgBox.style.transition = 'left 0.5s ease-in-out';
                imgBox.style.left = -index * bannerWidth + 'px';
            } else {
                index++;
                imgBox.style.transition = 'left 0.5s ease-in-out';
                imgBox.style.left = -index * bannerWidth + 'px';
            }
        }
        if (Math.abs(distanceX) > 0) {
            imgBox.style.transition = 'left 0.5s ease-in-out';
            imgBox.style.left = -index * bannerWidth + 'px';
        }
        //��ʼ��
        startX = 0;
        moveX = 0;
        distanceX = 0;
    })
    window.addEventListener('webkitTransitionEnd',function () {
        if (index == count - 1) {
            index = 1;
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + 'px';
        } else if (index == 0) {
            index = count - 2;
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + 'px';
        }
        circleLight(index);
        setTimeout(function () {
            isEnd = true;
            clearInterval(timerId);
            //������ʱ��
            startTime();
        },300)
    })
}
*/

window.onload = function () {
    searchScroll();
    startMove();
    gettime()
}
function searchScroll () {
    var search = document.querySelector('.jd_search');
    var banner = document.querySelector('.jd_banner');
    window.onscroll = function () {
        var bannerHeight = banner.offsetHeight;
        var scrollTop = document.documentElement.scrollTop;
        var opacity = scrollTop / bannerHeight;
        if (scrollTop <= bannerHeight) {
            search.style.backgroundColor = "rgba(255, 35, 34, "+opacity+")"
        }

    }
}

function gettime (){
    var time = document.querySelector('.jd_time');
    var spans = time.querySelectorAll('span');
    var time1 = new Date();
    var time2 = new Date(2018,1,14);
    var totalTime = (time2 - time1) / 1000;
    var timer = setInterval(function () {
        totalTime--;
        if (totalTime < 0) {
            clearInterval(timer);
            return
        }
        var day = Math.floor(totalTime / 60 / 60 / 24);
        var hour = Math.floor(totalTime / 60 / 60 % 24);
        var minute = Math.floor(totalTime / 60 % 60);
        var second = Math.floor(totalTime % 60);
        spans[0].innerHTML = Math.floor(day / 10);
        spans[1].innerHTML = Math.floor(day % 10);
        spans[3].innerHTML = Math.floor(hour / 10);
        spans[4].innerHTML = Math.floor(hour % 10);
        spans[6].innerHTML = Math.floor(minute / 10);
        spans[7].innerHTML = Math.floor(minute % 10);
        spans[9].innerHTML = Math.floor(second / 10);
        spans[10].innerHTML = Math.floor(second % 10);
    },1000)
}

function startMove () {
    var banner = document.querySelector('.jd_search');
    var imgBox = document.querySelector('.banner_img');
    var bannerWidth = banner.offsetWidth;
    var first = imgBox.querySelector('li:first-of-type');
    var last = imgBox.querySelector('li:last-of-type');
    imgBox.appendChild(first.cloneNode(true));
    imgBox.insertBefore(last.cloneNode(true),first);
    var lis = imgBox.querySelectorAll('li');
    var count = lis.length;
    for (var i = 0; i < count; i++) {
        lis[i].style.width = bannerWidth + 'px';
    }
    imgBox.style.width = count * bannerWidth + 'px';
    imgBox.style.left = -bannerWidth + 'px';
    window.onresize = function () {
        bannerWidth = banner.offsetWidth;
        for (var i = 0; i < count; i++) {
            lis[i].style.width = bannerWidth + 'px';
        }
        imgBox.style.width = count * bannerWidth + 'px';
        imgBox.style.left = -bannerWidth + 'px';
    }
    //自动轮播
    var index = 1;
    var timerId;
    function startTime () {
        timerId = setInterval(function () {
            index++;
            imgBox.style.transition = 'left 0.3s ease-in-out';
            imgBox.style.left = -index * bannerWidth + 'px';
            setTimeout(function () {
                if (index == count - 1) {
                    index = 1;
                    imgBox.style.transition = 'none';
                    imgBox.style.left = -index * bannerWidth + 'px';
                }
            },300)
        },2000)
    }
    startTime();
    //圆点
    function circleLight(index) {
        var  circle = document.querySelector('ol').querySelectorAll('li');
        for (var i = 0; i < circle.length; i++) {
            circle[i].classList.remove('active')
        }
        circle[index - 1].classList.add('active');
    }

    var isEnd = true;
    var startX,moveX,distanceX;
    window.addEventListener('touchstart', function (e) {
        clearInterval(timerId);
        startX = e.targetTouches[0].clientX;
    })
    window.addEventListener('touchmove', function (e) {
        if (isEnd) {
            moveX = e.targetTouches[0].clientX;
            distanceX = moveX - startX;
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + distanceX + 'px';
        }
    })
    window.addEventListener('touchend', function (e) {
        isEnd = false;
        if (Math.abs(distanceX) > 100) {
            if (distanceX > 0) {
                index--;
                imgBox.style.transition = 'left 0.3s ease-in-out';
                imgBox.style.left = -index * bannerWidth + 'px';
            } else if (distanceX < 0) {
                index++;
                imgBox.style.transition = 'left 0.3s ease-in-out';
                imgBox.style.left = -index * bannerWidth + 'px';
            }
        } else if (Math.abs(distanceX) > 0) {
            imgBox.style.transition = 'left 0.3s ease-in-out';
            imgBox.style.left = -index * bannerWidth + 'px';
        }

    })
    window.addEventListener('webkitTransitionEnd', function () {
        if (index == count - 1) {
            index = 1
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + 'px';
        } else if (index == 0) {
            index = count - 2;
            imgBox.style.transition = 'none';
            imgBox.style.left = -index * bannerWidth + 'px';
        }
        circleLight(index);
        setTimeout(function () {
            isEnd = true;
            clearInterval(timerId)
            startTime();
        },300)
    })
}